<?php

/**
 * Description of OrderAction
 *
 * @author Nr Aziz
 */
class OrderAction extends CAction {

    public $modelClass;
    public $pkName;

    public function run($pk, $name, $value, $move) {
        $model = CActiveRecord::model($this->modelClass)->findByPk($pk);
        $table = $model->tableName();
        $ifs = true;
        if ($move === 'up') {
            $op = '<';
            $inOrder = 'DESC';
           
        } else if ($move === 'down') {
            $op = '>'; 
            $inOrder = 'ASC';

        }
/*
        $sql = "SELECT {$table}.{$name} FROM $table WHERE $this->pkName $op $pk ORDER BY $this->pkName $inOrder LIMIT 1";
        $order = Yii::app()->db->createCommand($sql)->queryScalar();


        $highestOrder = Yii::app()
                ->db
                ->createCommand("SELECT {$table}.{$name} FROM {$table} ORDER BY {$table}.{$name} DESC LIMIT 1")
                ->queryScalar();


        if ($move === 'up' && $model->{$name} != 0){
            $order2 = $order;
            $order -= 1;            
        }
        else if ($move === 'down' && $order != $highestOrder+1){
           $order2 = $order; 
           $order += 1;
        }
        $model->{$name} = $order;
        $model->save(FALSE);*/
/*
        $rows = Yii::app()->db->createCommand()
                ->select($this->pkName)
                ->from($table)
                ->where("{$table}.{$name} $op2 :id", array(':id'=>$value))
                ->order(" {$table}.{$name} DESC ")
                ->limit(1)
                ->queryRow();
        $pk2 = $rows[$this->pkName];*/
        
        $sql = "SELECT {$table}.{$name} FROM $table WHERE {$table}.{$name} $op $value ORDER BY {$table}.{$name} $inOrder LIMIT 1";
        $order = Yii::app()->db->createCommand($sql)->queryScalar();
        
        $sql2 = "SELECT $this->pkName FROM $table WHERE {$table}.{$name} $op $value ORDER BY {$table}.{$name} $inOrder LIMIT 1";
        $pk2 = Yii::app()->db->createCommand($sql2)->queryScalar();
        
        $highestOrder = Yii::app()
                ->db
                ->createCommand("SELECT {$table}.{$name} FROM {$table} ORDER BY {$table}.{$name} DESC LIMIT 1")
                ->queryScalar();
                
        $lowestOrder = Yii::app()
                ->db
                ->createCommand("SELECT {$table}.{$name} FROM {$table} ORDER BY {$table}.{$name} ASC LIMIT 1")
                ->queryScalar();
                
         if ($move === 'up' && $model->{$name} != 0){                 
            //$order2 = $order;
            /* if(($order+1) < $value)
                $order += 1;  */          
        }
        else if ($move === 'down' && $order != $highestOrder+1){
            /* if(($order-1) > $value)
                 $order -= 1;*/
           //$order2 = $order; 
           //$order += 1;
        }
         if ($move === 'up') {
            if($value != $lowestOrder )
                $ifs = true;
            else
                $ifs = false;
         }
          if ($move === 'down') {
              if($value != $highestOrder )
                $ifs = true;
            else
                $ifs = false;
          }
            
        if( $ifs == true){
            $connection = yii::app()->db;
            $sql = "UPDATE {$table} SET {$table}.{$name} = $order WHERE $this->pkName = {$pk}";
            $command = $connection->createCommand($sql);
            $command->execute();
        }
        
        
        
        
        if( $ifs == true){
            $connection2 = yii::app()->db;
            $sql = "UPDATE {$table} SET {$table}.{$name} = $value WHERE $this->pkName = {$pk2}";
            $command2 = $connection2->createCommand($sql);
            $command2->execute();
        }
        
        
    }

}

?>
